<?php
namespace app\models;

use yii\web\IdentityInterface;
use app\common\corelib\CacheHelper;
/**
* 
*/
class User extends BaseModel implements IdentityInterface
{

    public static function getDb()
    {
        return \Yii::$app->db;
    }

    /**
     * Finds an identity by the given ID.
     *
     * @param string|integer $id the ID to be looked for
     * @return IdentityInterface|null the identity object that matches the given ID.
     */
    public static function findIdentity($id)
    {
        $user = json_decode(CacheHelper::get('user_id_'.$id));
        if(!$user){
            echo json_encode(['status'=>0,'error'=>['errorno'=>401,'errormsg'=>"请登录"]]);
            exit;
        }
        return $user;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        $user = CacheHelper::get('user_token_'.$token);
        if(!$user){
            /*echo json_encode(['status'=>0,'error'=>['errorno'=>401,'errormsg'=>"请登录"]]);
            exit;*/
            return false;
        }
        return $user;
    }

    /**
     * @return int|string current user ID
     */
    public function getId()
    {
        return $this->user_id;
    }

    /**
     * @return string current user auth key
     *
     */
    public function getAuthKey()
    {
        return $this->open_id;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey)
    {
        return $this->getAuthKey() === $authKey;
    }

}
